package cn.iocoder.yudao.module.book.service.voice;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.voice.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.voice.VoiceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.voice.VoiceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 录音 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class VoiceServiceImpl implements VoiceService {

    @Resource
    private VoiceMapper voiceMapper;

    @Override
    public Integer createVoice(VoiceSaveReqVO createReqVO) {
        // 插入
        VoiceDO voice = BeanUtils.toBean(createReqVO, VoiceDO.class);
        voiceMapper.insert(voice);
        // 返回
        return voice.getVoiceId();
    }

    @Override
    public void updateVoice(VoiceSaveReqVO updateReqVO) {
        // 校验存在
        validateVoiceExists(updateReqVO.getVoiceId());
        // 更新
        VoiceDO updateObj = BeanUtils.toBean(updateReqVO, VoiceDO.class);
        voiceMapper.updateById(updateObj);
    }

    @Override
    public void deleteVoice(Integer id) {
        // 校验存在
        validateVoiceExists(id);
        // 删除
        voiceMapper.deleteById(id);
    }

    private void validateVoiceExists(Integer id) {
        if (voiceMapper.selectById(id) == null) {
            throw exception(VOICE_NOT_EXISTS);
        }
    }

    @Override
    public VoiceDO getVoice(Integer id) {
        return voiceMapper.selectById(id);
    }

    @Override
    public PageResult<VoiceDO> getVoicePage(VoicePageReqVO pageReqVO) {
        return voiceMapper.selectPage(pageReqVO);
    }

}